﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
    class UserPassCodeDA {

        private const string SP_GetByUserKey = "dbo.UserPassCode_GetByUserKey";
        private const string SP_Insert = "dbo.UserPassCode_Insert";
        private const string SP_Update = "dbo.UserPassCode_Update";
        private const string SP_UpdateExpiredDate = "dbo.UserPassCode_UpdateExpiredDate";

        /// <summary>
        /// Inserts the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="connectionName">Name of the connection.</param>
        /// <param name="dbCon">The db con.</param>
        /// <param name="dbTrans">The db trans.</param>
        /// <returns></returns>
        public static int Insert(UserPassCode item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "Code", item.Code);
                db.AddParameter(cmd, "ExpiredDate", item.ExpiredDate);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }
        /// <summary>
        /// Updates the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="connectionName">Name of the connection.</param>
        /// <param name="dbCon">The db con.</param>
        /// <param name="dbTrans">The db trans.</param>
        /// <returns></returns>
        public static int Update(UserPassCode item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
                db.AddParameter(cmd, "Code", item.Code);
                db.AddParameter(cmd, "ExpiredDate", item.ExpiredDate);
                db.AddParameter(cmd, "UserKey", item.UserKey);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

        /// <summary>
        /// Updates the expired date.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="connectionName">Name of the connection.</param>
        /// <param name="dbCon">The db con.</param>
        /// <param name="dbTrans">The db trans.</param>
        /// <returns></returns>
        public static int UpdateExpiredDate(UserPassCode item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_UpdateExpiredDate)) {
                db.AddParameter(cmd, "ExpiredDate", item.ExpiredDate);
                db.AddParameter(cmd, "UserKey", item.UserKey);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                }
            }
            return result;
        }

        /// <summary>
        /// Creates from reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public static UserPassCode CreateFromReader(IDataReader reader) {
            UserPassCode item = new UserPassCode();
            item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
            item.Code = ConvertUtils.ToString(reader["Code"]);
            item.ExpiredDate = ConvertUtils.ToDateTime(reader["ExpiredDate"]);
            return item;
        }

        /// <summary>
        /// Gets the by user key.
        /// </summary>
        /// <param name="userKey">The user key.</param>
        /// <param name="connectionName">Name of the connection.</param>
        /// <param name="dbCon">The db con.</param>
        /// <param name="dbTrans">The db trans.</param>
        /// <returns></returns>
        public static UserPassCode GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            UserPassCode result = null;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
                db.AddParameter(cmd, "UserKey", userKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = CreateFromReader(reader);
                    }
                }
            }
            return result;
        }


    }
}
